package com.rpc.provider;

import com.rpc.framework.Protocol;
import com.rpc.framework.URL;
import com.rpc.provider.api.UserService;
import com.rpc.provider.impl.UserServiceImpl;
import com.rpc.register.LocalRegister;
import com.rpc.register.RemoteMapRegister;

import java.net.InetAddress;
import java.net.UnknownHostException;
import java.util.Iterator;
import java.util.ServiceLoader;

/**
 * @Author:cxy
 * @Date:2021/1/21 20:56
 */
public class Provider {

    public static void main(String[] args) throws UnknownHostException {
        //1 注册服务
        URL url = new URL(InetAddress.getLocalHost().getHostAddress(), 8080);
        RemoteMapRegister.regist(UserService.class.getName(), url);
        //2 本地注册
        LocalRegister.regist(UserService.class.getName(), UserServiceImpl.class);
        //3  spi 启动配置的tomcat
        ServiceLoader<Protocol> serviceLoader = ServiceLoader.load(Protocol.class);
        Iterator<Protocol> iterator = serviceLoader.iterator();
        while (iterator.hasNext()) {
            Protocol protocol = iterator.next();
            protocol.start(url);
        }
//        final HttpProtocol protocol = new HttpProtocol();
//        protocol.start(url);

    }
}
